package com.team.cloudsharing.dao.remember;

import java.io.Serializable;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
public class RememberDAOImpl implements RememberDAO{
	
	final static Logger logger = Logger.getLogger(RememberDAOImpl.class);

	@Autowired
	SessionFactory sessionFactory;
	
	@Override
	@Transactional
	public long insertRemember(RememberDO rememberDO) {
		long rememberId = -1;
		Session session = null;
		if (rememberDO != null) {
			try {
				session = sessionFactory.openSession();
				Transaction tx = session.beginTransaction();
				session.saveOrUpdate(rememberDO);
				tx.commit();
				Serializable id = session.getIdentifier(rememberDO);
				rememberId = (Long) id;
			} catch (Exception e) {
				logger.error(e.getMessage());
			} finally {
				try {
					session.close();
				} catch (Exception e2) {
					logger.error(e2.getMessage());
				}
			}
		}
		return rememberId;
	}

	@Override
	public RememberDO findRememberByRememberToken(String rememberToken) {
		RememberDO rememberDO = null;
		Session session = null;
		try {
			session = sessionFactory.openSession();
			Criteria criteria = session.createCriteria(RememberDO.class);
			criteria.add(Restrictions.eq("rememberToken", rememberToken));
			rememberDO = (RememberDO) criteria.uniqueResult();
		} catch (Exception e) {
			logger.error(e.getMessage());
		} finally {
			try {
				session.close();
			} catch (Exception e2) {
				logger.error(e2.getMessage());
			}
		}
		return rememberDO;
	}

	@Override
	public long updateRemember(RememberDO rememberDO) {
		long rememberId = -1;
		Session session = null;
		if (rememberDO != null) {
			try {
				session = sessionFactory.openSession();
				Transaction tx = session.beginTransaction();
				session.saveOrUpdate(rememberDO);
				tx.commit();
				Serializable id = session.getIdentifier(rememberDO);
				rememberId = (Long) id;
			} catch (Exception e) {
				logger.error(e.getMessage());
			} finally {
				try {
					session.close();
				} catch (Exception e2) {
					logger.error(e2.getMessage());
				}
			}
		}
		return rememberId;
	}

	@Override
	public boolean deleteRemember(long userId) {
		boolean statusDelete = false;
		Session session = null;
		String sql = "DELETE FROM remember WHERE user_id = :userId";
		try {
			session = sessionFactory.openSession();
			Query query = session.createSQLQuery(sql);
			query.setParameter("userId", userId);
			session.beginTransaction();
			statusDelete = (query.executeUpdate() > 0);
			session.getTransaction().commit();
		} catch (Exception e) {
			statusDelete = false;
			logger.error(e.getMessage());
		} finally {
			try {
				session.close();
			} catch (Exception e2) {
				logger.error(e2.getMessage());
			}
		}
		return statusDelete;
	}

	@Override
	public String findRememberTokenByUserId(long userId) {
		RememberDO rememberDO = null;
		Session session = null;
		try {
			session = sessionFactory.openSession();
			Criteria criteria = session.createCriteria(RememberDO.class);
			rememberDO = (RememberDO) criteria.uniqueResult();
		} catch (Exception e) {
			logger.error(e.getMessage());
		} finally {
			try {
				session.close();
			} catch (Exception e2) {
				logger.error(e2.getMessage());
			}
		}
		return rememberDO.getRememberToken();
	}
	
	
	

}
